package com.solcoo.merchant.ui.member;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.android.common.util.StringUtil;
import com.android.common.view.OnPullListener;
import com.android.common.view.PullListView;
import com.solcoo.merchant.ApplicationExt;
import com.solcoo.merchant.Constant;
import com.solcoo.merchant.R;
import com.solcoo.merchant.base.BaseActivity;
import com.solcoo.merchant.base.task.TaskHelper;
import com.solcoo.merchant.base.task.TaskHelper.Callback;
import com.solcoo.merchant.base.task.TaskHelper.DefaultCallback;
import com.solcoo.merchant.model.Member;
import com.solcoo.merchant.model.MemberList;
import com.solcoo.merchant.ui.order.OrderMemberActivity;

public class MemberActivity extends BaseActivity implements
		OnPullListener<MemberList>, OnItemClickListener {

	MemberAdapter adapter;

	PullListView memberList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.member);
		memberList = (PullListView) findViewById(R.id.member_list);
		adapter = new MemberAdapter(this);
		memberList.setAdapter(adapter);
		memberList.setOnPullListener(this);
		memberList.setOnItemClickListener(this);
		TaskHelper.loadView(this, Constant.URL.getMemberList(
				ApplicationExt.getUser().yonghum, adapter.cursor), callback,
				MemberList.class);
	}

	private Callback<MemberList> callback = new DefaultCallback<MemberList>(
			this) {
		@Override
		public void onSuccess(MemberList result) {
			adapter.update(result);
			memberList.setVisibility(View.VISIBLE);
			memberList.setSelection(1);
			if (!adapter.hasMore()) {
				memberList.setFooterViewVisibility(View.GONE);
			}
		}
	};

	@Override
	public MemberList onTopRefreshingInBackground() {
		adapter.cursor = 0;
		return TaskHelper.syncGet(Constant.URL.getMemberList(
				ApplicationExt.getUser().yonghum, adapter.cursor),
				MemberList.class);
	}

	@Override
	public String onTopRefreshed(MemberList result) {
		adapter.update(result);
		if (!adapter.hasMore()) {
			memberList.setFooterViewVisibility(View.GONE);
			return getString(R.string.pull_list_footer_no_more_toast_msg);
		}
		return StringUtil.EMPTY;
	}

	@Override
	public MemberList onBottomLoadingInBackground() {
		return TaskHelper.syncGet(Constant.URL.getMemberList(
				ApplicationExt.getUser().yonghum, adapter.cursor),
				MemberList.class);
	}

	@Override
	public String onBottomLoaded(MemberList result) {
		adapter.add(result);
		if (!adapter.hasMore()) {
			memberList.setFooterViewVisibility(View.GONE);
			return getString(R.string.pull_list_footer_no_more_toast_msg);
		}
		return StringUtil.EMPTY;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		Member member = (Member) parent.getItemAtPosition(position);
		Intent it = new Intent(v.getContext(), OrderMemberActivity.class);
		it.putExtra(OrderMemberActivity.INTENT_KEY, member.yonghum);
		v.getContext().startActivity(it);
	}
}
